import com.dmotiko.general.*

class FormField
extends BaseClip {
		
	private var txtLabel:TextField;	
	private var txtInput:TextField;
	private var mcError:MovieClip;
	private var validator:Function;
		
	private function FormField() {
		super();
	}
	
	private function initClip():Void {
		var root:FormField = this;
		if (mcError) {
			txtInput.onSetFocus = function() {
				root.hideError();
			}
		}
		txtInput.onKillFocus = function() {
			root.setData( root.txtInput.text );
		}
	}
	
	public function setLabel(sLabel:String):Void {
		this.txtLabel.text = sLabel;
	}
	
	public function setTabIndex(nIndex:Number):Void {
		this.txtInput.tabIndex = nIndex;
	}
	
	public function restrictInput( chars:String, maxChars:Number ):Void {
		txtInput.restrict = chars;
		if (maxChars) txtInput.maxChars = maxChars;
	}
	
	public function erase():Void {
		this.txtInput.text = "";
		hideError();
		setData( txtInput.text );
	}
	
	public function setValidator(validator:Function):Void {
		this.validator = validator;
	}
	
	public function validate():Boolean {
		var b:Boolean = true;
		if (validator) b = validator( txtInput.text );
		if ( !b ) showError();
		else setData( txtInput.text );
		return b;
	}
	
	private function refreshData():Void {
		dispatchEvent( { target: this, type: BaseClipEvent.CHANGED } );
	}
	
	private function showError():Void {
		if (mcError) mcError.gotoAndPlay("show");
	}
	private function hideError():Void {
		if (mcError) mcError.gotoAndPlay("hide");
	}
	
}